var quelist;
var idSum;

function getdata(){
	$.ajax({
		url: all_baseurl+"getQuestions",
		type: "get",
		dataType: "json",
		async: false,
		data:{},
		success: function (data) {
			idSum=data.idSum;
			quelist=data.questionList;
			for(var i=0;i<quelist.length;i++){
				var strlist=quelist[i].option.split(";");
				quelist[i].option=strlist;
				quelist[i].imgurl="<img src='"+all_baseurl+"upload/"+quelist[i].imgurl+"' style='width:150px;height:150px;padding-left: 40%'>";
				quelist[i].sign=i+1;
				quelist[i].title="读音为'"+quelist[i].zhuang+"'的壮字汉语语义是:"
			}
		},
		error: function (error) {
		}
	})
}

function postdata(answertrue){
	$.ajax({
		url: all_baseurl+"uploadTopic",
		type: "get",
		dataType: "json",
		async: false,
		data:{
			num:answertrue
		},
		success: function (data) {
		},
		error: function (error) {
		}
	})
}

function getover(answertrue){
	var sum=0;
	for(var i=0;i<answertrue;i++){
		sum+=idSum[i].sum;
	}
	return sum;
}

function TiMu() {
	for (var i in quelist) {
		var div = document.createElement("div");
		div.className = "entrance-bottom-frame-line";
		document.querySelector(".entrance-bottom-frame").appendChild(div);

		var beijing = document.createElement("div");
		beijing.className = "entrance-bottom-frame-beijing";
		document.querySelectorAll(".entrance-bottom-frame-line")[i].appendChild(beijing);

		var div1 = document.createElement("div");
		div1.className = "entrance-bottom-frame-line-id";
		div1.innerHTML = quelist[i].sign;
		document.querySelectorAll(".entrance-bottom-frame-line")[i].appendChild(div1);

		var div2 = document.createElement("div");
		div2.className = "entrance-bottom-frame-line-title";
		div2.innerHTML = quelist[i].title;
		document.querySelectorAll(".entrance-bottom-frame-line")[i].appendChild(div2);

		var code = document.createElement("code");
		var pre = document.createElement("pre");
		code.innerHTML = quelist[i].imgurl;
		pre.appendChild(code);
		pre.className = "pre_message"
		document.querySelectorAll(".entrance-bottom-frame-line")[i].appendChild(pre);

		for (var j in quelist[i].option) { //选项
			var div3 = document.createElement("div");
			div3.className = "entrance-bottom-frame-line-button";
			var div3_id = document.createElement("div");
			div3_id.className = "entrance-bottom-frame-line-button-id";
			if (j == 0) {
				div3_id.innerHTML = "A";
			} else if (j == 1) {
				div3_id.innerHTML = "B";
			} else if (j == 2) {
				div3_id.innerHTML = "C";
			} else {
				div3_id.innerHTML = "D";
			}
			var div4 = document.createElement("div");
			div4.className = "entrance-bottom-frame-line-button-frame";
			div4.innerHTML = quelist[i].option[j];
			div3.appendChild(div3_id)
			div3.appendChild(div4);
			document.querySelectorAll(".entrance-bottom-frame-line")[i].appendChild(div3);
		}
	}

}

window.onload = function () {
	getdata()
	TiMu()
	var dact = document.querySelector(".entrance-bottom-frame-line")
	var dactall_len = document.querySelectorAll(".entrance-bottom-frame-line-button").length;
	addClass(dact, 'active')
	var timu_id = 0
	var answertrue = 0
	var tf=0    //判断最后一题正确
	for (var i = 0; i < dactall_len; i++) {
		//选项点击触发事件
		document.querySelectorAll(".entrance-bottom-frame-line-button")[i].onclick = function (event) {
			if (timu_id < document.querySelectorAll(".entrance-bottom-frame-line").length - 1) {
				timu_id++;
				if (quelist[timu_id-1].chinese == event.srcElement.innerHTML) {
					poplayer.alert('回答正确!', timu_id);
					answertrue++
				} else {
					poplayer.alert('回答错误!正确答案是: ' + quelist[timu_id-1].chinese, timu_id);
				}
			} else {
				if (quelist[timu_id].chinese == event.srcElement.innerHTML) {
					answertrue++
					tf=1;
				}

				var num = (answertrue * 100) / quelist.length;
				num = num.toFixed(2);

				postdata(answertrue);

				var sum=getover(answertrue);

				if(tf){
					poplayer.confirm(
						'回答正确!' + '<br>'
						+ '答题结束!' + '<br>'
						+ '答对:' + answertrue + '<br>'
						+ "准确率:" + num + "%<br>"
						+ "超过了" + sum + "人", '确定', '取消', all_baseurl);
				}
				else{
					poplayer.confirm(
						'回答错误!正确答案是: ' + quelist[timu_id].chinese + '<br>'
						+ '答题结束!' + '<br>'
						+ '答对:' + answertrue + '<br>'
						+ "准确率:" + num + "%<br>"
						+ "超过了" + sum + "人", '确定', '取消', all_baseurl);
				}
			}
		}
	}
}